package com.atguigu.tingshu.live.api;

import org.joda.time.LocalDateTime;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;

/**
 * @author atguigu-mqx
 * @ClassName Test
 * @description: TODO
 * @date 2023年12月15日
 * @version: 1.0
 */
public class Test {

    public static void main(String[] args) {

        //  可以有默认值live
        //  String appName="";
        String streamName = "room1";
        //  鉴权key = ?txSecret=15ac283f6bf507ed090a00b8ee9aee23&txTime=657D3F2E
        //  txSecret=2133c689c452f0bf81b1a88261f1d9a6&txTime=579ACB15
        //  System.out.println(getSafeUrl("txrtmp", "11212122", 1469762325L));
        //  默认增加一天：
        //  LocalDate localDate = LocalDateTime.now().plusDays(1).toLocalDate();
        long txTime = LocalDateTime.now().plusDays(1).toDate().getTime()/1000;
        //  主Key
        String key = "3df6a2b8b90c69391c43520be027b7bb";
        String txSecret = getSafeUrl(key, streamName, txTime);
        //  webrtc://domain/AppName/StreamName?txSecret=Md5(key+StreamName+hex(time))&txTime=hex(time)
        //  webrtc://190649.push.tlivecloud.com/live/room1?txSecret=15ac283f6bf507ed090a00b8ee9aee23&txTime=657D3F2E 自动生成!
        //  webrtc://190649.push.tlivecloud.com/live/room1?txSecret=b1552e58b81e1c3e29863107bbbbcdef&txTime=657D4077
        String pushUrl = "webrtc://190649.push.tlivecloud.com/live/"+streamName+"?"+txSecret;
        System.out.println(pushUrl);

        String pullUrl = "webrtc://mqxplay.atguigu.cn/live/"+streamName+"?"+txSecret;
        System.out.println(pullUrl);
    }

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /*
     * KEY+ streamName + txTime
     */
    private static String getSafeUrl(String key, String streamName, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamName).
                append(Long.toHexString(txTime).toUpperCase()).toString();

        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return txSecret == null ? "" :
                new StringBuilder().
                        append("txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }

    private static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}
